<?php

namespace app\member\controller;

use app\admin\model\Login as ModelLogin;
use app\common\controller\Frontend;


class Register extends Frontend
{
    //TP5布局
    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = 'default';

    public function index()
    {
        if (session("memberinfo")) {
            $this->error("请勿重复登陆");
        }
        $url = $this->request->request('url', '');
        $this->view->assign('url', $url);
        $this->view->assign('title', __('Register'));
        return $this->view->fetch();
    }

    // 注册提交
    public function regdata()
    {
        if (session("memberinfo")) {
            $this->error("请勿重复登陆");
        }
        $username = input('username','','trim');
        $password = input('password','','trim');
        $mobile = input('mobile','','trim');
        if (empty($username)) {
            $this->error('用户名不能为空');
        }
        if (empty($password)) {
            $this->error('密码不能为空');
        }
        if (empty($mobile)) {
            $this->error('手机号不能为空');
        }
        // 判断手机号格式是否正确
        $mobilereg = "/^1[3456789]\d{9}$/";
        if (!preg_match($mobilereg, $mobile)) {
            $this->error('手机号格式不正确');
        }
        // 判断唯一：手机号唯一、用户名唯一
        $isexisMobile = ModelLogin::where('mobile', $mobile)->find();
        if ($isexisMobile) {
            $this->error('手机号已被注册');
        }
        $isexisName = ModelLogin::where('username', $username)->find();
        if ($isexisName) {
            $this->error('用户名已被注册');
        }

        $memdata = [
            'username' => $username,
            'password' => md5($password),
            'mobile'   => $mobile,
        ];
        $res = ModelLogin::create($memdata);
        if ($res !== false) {
            $this->success("注册成功");
        } else {
            $this->error("注册失败,请重新注册");
        }
    }
}